import { observable, action, autorun, toJS } from 'mobx';
import { WeaTools, WeaLocaleProvider } from 'ecCom';
import { message, Modal } from 'antd';
import * as Apis from '../apis/highSeas';
import * as mApis from '../apis/mineCustomer';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

export default class HighSeasSettingListStore {
    @observable tableSessionKey = '';
    @observable tableStore = new TableStore();
    @observable loading = false;
    @observable searchConditionForm = new WeaForm();
    @observable searchConditionComponents = {};
    @observable showSearchAd = false;
    @observable searchBaseParams = {};
    @observable showLeft = true;
    @observable rightMenus = [];
    @observable hrmList = [];
    @observable dialogInfo = { visible: false, title: getLabel('386715', '公海信息'), loading: false, disabled: false };
    @observable hrmid = '';
    @observable operate = '';
    @observable editId = '';
    @observable hasSeasRight = false;
    @observable verified = false;
    @observable selectedKeys = [];
    @observable logDialogVisible = false;
    @observable logDialogSeasId = '';
    @observable CrmLogStore = {};
    @observable onOperates = true;
    @observable highSeasSettingFormStore;

    @action
    initDatas = (params = {}) => {
      this.highSeasSettingFormStore = params;
      this.CrmLogStore = new weaCrmEngine.com.CrmLogStore();
      this.searchBaseParams = {};
      this.tableStore = new TableStore();
      this.searchConditionForm.reset();
      mApis.getSeasRight({}).then(action((data) => {
        if (data.datas) {
          this.verified = true;
          this.hasSeasRight = true;
          this.loading = true;
          this.getTableData({ onlysessionkey: false });
          this.getHrmList();
        } else {
          this.verified = true;
          this.hasSeasRight = false;
        }
      }));
    }

    @action
    onLeftChange =(ids) => {
      this.searchConditionForm.reset();
      this.searchConditionForm.updateFields({ manager: ids }, false);
      this.searchBaseParams = { manager: ids };
      this.doSearch();
    }

    @action
    doSearch = () => {
      this.tableStore = new TableStore();
      const that = this;
      this.loading = true;
      this.setShowSearchAd(false);
      const searchParamsAd = this.searchConditionForm.getFormParams();
      const requestParams = { ...this.searchBaseParams, ...searchParamsAd, onlysessionkey: true };
      for (const item in requestParams) {
        if (requestParams[item] == '') {
          delete requestParams[item];
          delete that.searchBaseParams[item];
        }
      }

      this.searchBaseParams = { ...this.searchBaseParams, ...requestParams };
      this.getTableData(this.searchBaseParams);
    }

    getTableData = (params = {}) => {
      Apis.getHighSeasInfo('getSeasList', params).then((data) => {
        if (data.status == '0') {
          message.error(`${getLabel(83437, '出现错误！') + data.error}!`);
        } else {
          this.tableSessionKey = data.datas;
          this.tableStore.getDatas(data.datas, 1);
          if (!params.onlysessionkey) {
            this.searchConditionComponents = data.formItemGroupList;
            this.searchConditionForm.initFormFields(this.searchConditionComponents);

            this.rightMenus = data.rightMenu;
          }
        }
        this.loading = false;
      });
    }

    @action
    tableReload = () => {
      this.tableStore.getDatas(this.tableSessionKey, this.tableStore.current);
    }

    @action
    setShowSearchAd = (bool = false) => {
      this.showSearchAd = bool;
    }

    clearAdFields = () => {
      this.searchConditionForm.reset();
      delete this.searchBaseParams.name;
    }

    @action
    setSearchKey = (v = '') => {
      try {
        this.searchBaseParams.name = v;
        const searchKeyField = {};
        searchKeyField.name = v;
        this.searchConditionForm.updateFields(searchKeyField, false);
      } catch (error) {
        // 高级搜索中没有searchKey的情况
      }
    }

    @action
    searchConditionFieldChange = (fieldObj) => {
      if (fieldObj.name) {
        this.searchBaseParams = { ...this.searchBaseParams, name: fieldObj.name.value };
      }
    }

    @action
    isShowLeft = (bl) => {
      this.showLeft = bl;
    }

    @action
    controlOperateDialog = (operate, askfn, params) => {
      mApis.getSeasRight({}).then(action((data) => {
        if (data.datas) {
          if (operate == 'add') {
            this.operate = 'add';
            this.dialogInfo.visible = true;
            this.dialogInfo.title = getLabel('386716', '新建公海');
          } else if (operate == 'delete') {
            const that = this;
            Modal.confirm({
              title: getLabel(33475, '确认要删除选中记录吗？'),
              onOk() {
                const requestParams = { ids: `${params}`, operate };
                Apis.getHighSeasInfo('doSaveForm', requestParams).then((data) => {
                  message.success(getLabel('83472', '删除成功！'));
                  that.tableReload();
                });
              },
              onCancel() {},
            });
          } else if (operate == 'edit') {
            this.operate = 'edit';
            this.editId = params.id;
            this.dialogInfo.visible = true;
            this.dialogInfo.title = getLabel('386717', '编辑公海');
          } else if (operate == 'save') {
            // this.dialogInfo.disabled = true;
            if (this.checkForm(toJS(params))) {
              delete params.subcompanyReplaceDatas;
              delete params.managerReplaceDatas;
              delete params.departmentReplaceDatas;
              this.loading = true;
              Apis.getHighSeasInfo('doSaveForm', params).then((data) => {
                this.loading = false;
                if (data.status == '1') {
                  if (params.operate == 'add') {
                    this.operate = 'edit';
                    if (askfn)eval(askfn({ editId: data.datas, operate: 'edit' }));// 新建后，将弹出改为编辑
                    this.dialogInfo.visible = true;
                    this.dialogInfo.title = getLabel('386717', '编辑公海');
                    message.success(getLabel('83551', '保存成功！'));
                    // message.success(getLabel('386718',"保存成功！您可继续设置公海权限！"));
                  } else {
                    this.dialogInfo.visible = false;
                    message.success(getLabel('83551', '保存成功！'));
                  }
                  this.dialogInfo.disabled = false;
                  this.tableReload();
                } else {
                  message.error(getLabel(83437, '出现错误！'));
                }
              });
            } else {
              this.operate = params.operate;
            }
            // else{
            // this.dialogInfo.disabled = false;
            // Modal.info({
            //     title:getLabel('15172',"系统提示"),
            //     content:getLabel('30933',"必填信息不完整！")
            // });
            // return;
            // }
          } else if (operate == 'save2close') {
            // this.dialogInfo = {visible:false};
          } else if (operate == 'toback') {
            this.dialogInfo.visible = false;
            this.errorRefs = { name: {}, manager: {}, commonUnrelated: {}, seasUnrelated: {}, warnDay: {}, warnRemind: {} };
          } else if (operate == 'showlog') {
            this.CrmLogStore.show(true, '502', params.seasId);
          } else if (operate == 'showMonitorLog') {
            this.logDialogVisible = true;
            this.logDialogSeasId = params.seasId;
          }
        } else {
          this.hasSeasRight = false;
        }
      }));
    }


    checkForm = params => this.highSeasSettingFormStore.checkForm(params)

    @action
    getHrmList = (params = {}) => {
      Apis.getHrmList(params).then((data) => {
        if (data.status == '0') {
          message.error(`${getLabel(83437, '出现错误！') + data.error}!`);
        } else {
          this.hrmList = data.datas;
        }
      });
    }

    // @action
    // hrmid = (hrmid)=>{
    //     this.hrmid = hrmid;
    // }

    @action
    doSynchroniz = (seasId) => {
      mApis.getSeasRight({}).then(action((data) => {
        if (data.datas) {
          this.loading = true;
          this.onOperates = false;
          const params = { seasId };
          // message.warn(getLabel('385143',"正在同步，请稍候..."));
          Apis.getHighSeasInfo('doSeasSynchroniz', params).then((data) => {
            if (data.status == 1) {
              message.success(data.msg);
              this.tableReload();
            } else {
              message.error(data.msg);
            }
            this.loading = false;
            this.onOperates = true;
          });
        } else {
          this.hasSeasRight = false;
        }
      }));
    }

    @action
    changeLogDialog = () => {
      this.logDialogVisible = false;
      this.logDialogSeasId = '';
    }
}
